package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.AdminUserDao;
import com.ws.shop.entity.AdminUser;
import com.ws.shop.util.StringUtil;

@Repository(value="adminUserDao")
public class AdminUserDaoImpl extends BaseDaoImpl implements AdminUserDao {
	private static Logger log = Logger.getLogger(AdminUserDaoImpl.class);
	
	String save_sql = "insert into admin_user(username,password,realname,descs,status,roleid,creatid,creattime) values(?,?,?,?,?,?,?,now())";
	String update_pwd_sql = "update admin_user set password=?,realname=?,descs=?,status=?,roleid=? where userid=?";
	String update_sql = "update admin_user set realname=?,descs=?,status=?,roleid=? where userid=?";
	public void saveOrUpdate(AdminUser adminUser) {
		if(adminUser.getUserid() == 0){
			this.update(save_sql,adminUser.getUsername(),adminUser.getPassword(),
					adminUser.getRealname(),adminUser.getDescs(),adminUser.getStatus(),
					adminUser.getRoleid(),adminUser.getCreatid());
		}else if(adminUser.getPassword() != null){
			this.update(update_pwd_sql, adminUser.getPassword(), adminUser.getRealname(),adminUser.getDescs(),adminUser.getStatus(),adminUser.getRoleid(),adminUser.getUserid());
		}else{
			this.update(update_sql, adminUser.getRealname(),adminUser.getDescs(),adminUser.getStatus(),adminUser.getRoleid(),adminUser.getUserid());
		}
	}

	public Object[] deleteAdminUserById(int userid) {
		this.update("delete from admin_user where userid=?", userid);
		return new Object[]{0,"删除成功"};
	}

	public AdminUser findAdminUserById(final int userid) {
		String sql = "select username,password,realname,descs,status,roleid,creatid,creattime from admin_user where userid=?";
		return this.query(sql, new ResultSetExtractor<AdminUser>(){
			public AdminUser extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if(!rs.next())
					return null;
				AdminUser adminUser = new AdminUser();
				adminUser.setUserid(userid);
				adminUser.setUsername(rs.getString(1));
				adminUser.setPassword(rs.getString(2));
				adminUser.setRealname(rs.getString(3));
				adminUser.setDescs(rs.getString(4));
				adminUser.setStatus(rs.getInt(5));
				adminUser.setRoleid(rs.getInt(6));
				adminUser.setCreatid(rs.getInt(7));
				adminUser.setCreattime(rs.getTimestamp(8));
				return adminUser;
			}
		}, userid);
	}

	final String getAdminUserListByPage_count = "select count(1) from admin_user u";
	final String getAdminUserListByPage_list = "select u.userid,u.username,u.realname realname,u.descs,u.status,u.creattime,up.realname crealname,r.name rolename from admin_user u left join admin_user up on u.creatid=up.userid left join admin_role r on u.roleid=r.roleid";
	final String getAdminUserListByPage_order = " order by u.userid desc";
	public Page getAdminUserListByPage(int pageno, int pagesize, String username,
			int status) {
		StringBuilder query = new StringBuilder(" where 1=1");
		if(!StringUtil.isEmpty(username)){
			query.append(" and u.username='").append(username).append("'");
		}
		if(status >= 0){
			query.append(" and u.status=").append(status);
		}
		return findPage(pageno,pagesize,new StringBuilder(getAdminUserListByPage_count).append(query).toString()
				,new StringBuilder(getAdminUserListByPage_list).append(query).append(getAdminUserListByPage_order));
	}

	public AdminUser findByUsername(final String username) {
		return this.query("select userid,password,realname,descs,status,roleid,creatid,creattime from admin_user where username=?", new ResultSetExtractor<AdminUser>(){
			public AdminUser extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if(!rs.next())
					return null;
				AdminUser adminUser = new AdminUser();
				adminUser.setUsername(username);
				adminUser.setUserid(rs.getInt(1));
				adminUser.setPassword(rs.getString(2));
				adminUser.setRealname(rs.getString(3));
				adminUser.setDescs(rs.getString(4));
				adminUser.setStatus(rs.getInt(5));
				adminUser.setRoleid(rs.getInt(6));
				adminUser.setCreatid(rs.getInt(7));
				adminUser.setCreattime(rs.getTimestamp(8));
				return adminUser;
			}
		}, username);
	}

	public void updateAdminUserPassword(int userid, String password) {
		this.update("update admin_user set password=? where userid=?", password,userid);
	}

}
